package cn.itcast.controller;

import cn.itcast.service.IChatHistoryService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai/service")
public class ServiceChatController {
    @Autowired
    private ChatClient serviceChatClient;

    @Autowired
    private IChatHistoryService chatHistoryService;

    /**
     * 响应式流-html
     * @param prompt
     * @return
     */
    @GetMapping( produces = "text/html;charset=UTF-8")
    public Flux<String> stream(@RequestParam("prompt") String prompt, @RequestParam("chatId") String chatId){
        chatHistoryService.saveChatHistory("chat",chatId);
        return  serviceChatClient.prompt(prompt)
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId))
                .stream().content();
    }

}
